﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hiro.Interfaces;

namespace Hiro.ActivationPoints
{
    /// <summary>
    /// Represents a class that adds helper methods to the <see cref="IDependencyMap"/> interface.
    /// </summary>
    public static class DependencyMapExtensions
    {
        /// <summary>
        /// Adds an anonymous service to the dependency map.
        /// </summary>
        /// <param name="map">The target dependency map.</param>
        /// <param name="serviceType">The service type to implement.</param>
        /// <param name="implementingType">The concrete type that implements the service type.</param>
        public static void AddService(this IDependencyMap map, Type serviceType, Type implementingType)
        {
            map.AddService(null, serviceType, implementingType);
        }

        /// <summary>
        /// Adds a service to the dependency map.
        /// </summary>
        /// <param name="map">The target dependency map.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="serviceType">The service type to implement.</param>
        /// <param name="implementingType">The concrete type that implements the service type.</param>
        public static void AddService(this IDependencyMap map, string serviceName, Type serviceType, Type implementingType)
        {
            var dependency = new Dependency(serviceName, serviceType);
            var activationPoint = implementingType.ToActivationPoint();

            map.AddService(dependency, activationPoint);
        }

        /// <summary>
        /// Adds a service to the dependency map.
        /// </summary>
        /// <typeparam name="TService">The service name.</typeparam>
        /// <typeparam name="TImplementation">The concrete type that implements the service type.</typeparam>
        /// <param name="map">The target dependency map.</param>
        public static void AddService<TService, TImplementation>(this IDependencyMap map)
            where TService : class, TImplementation
        {
            map.AddService<TService, TImplementation>(null);
        }

        /// <summary>
        /// Adds a service to the dependency map.
        /// </summary>
        /// <typeparam name="TService">The service name.</typeparam>
        /// <typeparam name="TImplementation">The concrete type that implements the service type.</typeparam>
        /// <param name="map">The target dependency map.</param>
        /// <param name="serviceName">The service name.</param>
        public static void AddService<TService, TImplementation>(this IDependencyMap map, string serviceName)
            where TService : class, TImplementation
        {
            map.AddService(serviceName, typeof(TService), typeof(TImplementation));
        }
    }
}
